// This BDCCBullet is a simple overlay that shows a solid circlular area on the map using a single bullet character
// You can specify the color and opacity of the circle.
// The radius of the circle is given in meters.
//
// Events are 'click','mouseover' and 'mouseout' 
//
// (c) Bill Chadwick 2010

function BDCCBullet(point, radiusMtr, fillColor, fillOpacity, tooltip) {

  this.point_ = point;
  this.radiusMtr_ = radiusMtr;  
  this.fillColor_ = fillColor || "#444444";
  this.fillOpacity_ = fillOpacity || 0.3;
  this.tooltip_ = tooltip;
  this.mouseOverChar_ = false;
  this.mouseOverBullet_ = false;
  this.radiusPxSquared_ = 0;

}
BDCCBullet.prototype = new GOverlay();

//Get/Set methods

//Point or center
BDCCBullet.prototype.getPoint = function(){
    return this.point_;
}
BDCCBullet.prototype.setPoint = function(point){
    this.point_ = point;
    this.redraw(false);
}

// Radius
BDCCBullet.prototype.getRadiusMtr = function(){
    return this.radiusMtr_;
}
BDCCBullet.prototype.setRadiusMtr = function(radiusKm){
    this.radiusKm_ = radiusMtr;
    this.redraw(false);
}

//Fill color
BDCCBullet.prototype.setFillColor = function(fillColor) {
  this.fillColor_ = fillColor || "#444444";
  this.redraw(false);
}
BDCCBullet.prototype.getFillColor = function() {
    return this.fillColor_;
}

//Fill opacity 0.0 (transparent) to 1.0 (opaque)
BDCCBullet.prototype.setFillOpacity = function(opacity) {
    this.fillOpacity_ = opacity || 0.3;
    this.redraw(false);
}
BDCCBullet.prototype.getFillOpacity = function() {
    return this.fillOpacity_;
}

//Event handlers/posters 
BDCCBullet.prototype.onClick = function(e) {
    if(this.mouseOverBullet_){
        GEvent.trigger(this,"click",e);
    }
}
BDCCBullet.prototype.onOver = function() {
    this.mouseOverChar_ = true;
}
BDCCBullet.prototype.onOut = function() {
    if (this.mouseOverBullet_) {
        this.mouseOverBullet_ = false;
        if (this.tooltip_) {
            this.div_.title = "";
            this.div_.style.cursor = "";
        }
        GEvent.trigger(this, "mouseout");
    }
    this.mouseOverChar_ = false;
}
BDCCBullet.prototype.onMove = function(e) {

    if (this.mouseOverChar_) {

        //browser independant get of mouse coords
        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft
			            + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
			            + document.documentElement.scrollTop;
        }

        // get mouse coords in map pixel coords
        var p = this.map_.getContainer();
        posx -= p.offsetLeft;
        posy -= p.offsetTop;

        // Get the coords of the centre point of our circle
        var pt = this.map_.fromLatLngToContainerPixel(this.point_);

        // mouse coords within the bullet ?
        var dx = (posx - pt.x);
        var dy = (posy - pt.y);
        var d2 = (dx * dx) + (dy * dy);
        if (d2 < this.radiusPxSquared_) {
            if (!this.mouseOverBullet_){
                this.mouseOverBullet_ = true;
                if (this.tooltip_) {
                    this.div_.title = this.tooltip_;
                    this.div_.style.cursor = "help";
                }
                GEvent.trigger(this, "mouseover");
            }
        }
        else if (this.mouseOverBullet_) {
            this.mouseOverBullet_ = false;
            if (this.tooltip_) {
                this.div_.title = "";
                this.div_.style.cursor = "";
            }
            GEvent.trigger(this, "mouseout");
        }
    }
}


// Creates the DIV representing this circle.
BDCCBullet.prototype.initialize = function(map) {

  //save for later
  this.map_ = map;

  //set up invariant details  
  var c = document.createElement("DIV");
  c.style.position = "absolute";
  c.style.overflow = "hidden";
  c.style.border = "0px";
  c.style.padding = "0px";
  c.style.margin = "0px";
  c.style.border = "none";
  c.style.fontFamily='arial,sans-serif';
  c.innerHTML = "<span>&#9679</span>";//password dot, bigger than a bullet
  c.style.display="none";//until redraw
  map.getPane(G_MAP_MAP_PANE).appendChild(c);

  //closures for dom event handlers
  var eClick = GEvent.callback(this, this.onClick);
  var eOver = GEvent.callback(this, this.onOver);
  var eOut = GEvent.callback(this, this.onOut);
  var eMove = GEvent.callback(this, this.onMove);
    
  GEvent.clearInstanceListeners(c);//safety 
  GEvent.addDomListener(c,"click",function(e){eClick(e);});
  GEvent.addDomListener(c,"mouseover",function(){eOver();});
  GEvent.addDomListener(c,"mouseout",function(){eOut();});
  GEvent.addDomListener(c,"mousemove", function(e) { eMove(e); });
  
  this.div_ = c;        
}

// Remove the bullet DIV from the map pane
BDCCBullet.prototype.remove = function() {

  if (this.div_ != null){
    GEvent.clearInstanceListeners(this.div_);//safety 
    this.map_.getPane(G_MAP_MAP_PANE).removeChild(this.div_);
    this.div_ = null;
  }
    
}

// Copy our data to a new BDCCBullet
BDCCBullet.prototype.copy = function() {
  return new BDCCBullet(
      this.point_, 
      this.radiusMtr_,  
      this.fillColor_, 
      this.fillOpacity_, 
      this.tooltip_ 
  );
}

// Redraw the circle based on the current projection and zoom level
BDCCBullet.prototype.redraw = function(force) {

    // Get the DIV coordinates of the centre point of our circle
    var p = this.map_.fromLatLngToDivPixel(this.point_);

    //get the scale of the map in meters per pixel
    var sz = this.map_.getSize();
    var bnds = this.map_.getBounds();
    var pxDiag = Math.sqrt((sz.width * sz.width) + (sz.height * sz.height));
    var mtrDiag = bnds.getNorthEast().distanceFrom(bnds.getSouthWest());
    var pxPerMtr = pxDiag / mtrDiag;

    //get the radius of the bullet in px
    var rPx = this.radiusMtr_ * pxPerMtr;
    this.radiusPxSquared_ = rPx * rPx;

    if (rPx > 0) {

        this.div_.style.display = "";
        this.div_.style.fontSize = (rPx / 3.87).toFixed(0) + "em"; //scaling suits most PC browsers
        this.div_.style.color = this.fillColor_;
        //this.div_.style.backgroundColor = "#404040";//test
        this.div_.style.opacity = this.fillOpacity_;
        this.div_.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(this.fillOpacity_ * 100, 10).toString() + ")";
        var t = p.y - (this.div_.clientHeight * 0.555); // offsets from centre of character in text cell
        var l = p.x - (this.div_.clientWidth * 0.500);
        this.div_.style.left = l.toFixed(0) + "px";
        this.div_.style.top = t.toFixed(0) + "px";

        if ((this.div_.clientWidth * 0.500) < rPx) {
            //this.div_.style.display = "none"; // don't render if not big enough (Firefox limits font size to 2000px), other browsers don't
        }
    }
    else {
        this.div_.style.display = "none";
    }
}